home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Components / Microsoft ASP / _SETUP.1 / MSDBUpdate.inc < prev    next >
Encoding:
Text File  |  1998-11-20  |  17.7 KB  |  530 lines

  1. <SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
  2. <!--#INCLUDE FILE="MSGlobal.inc"-->
  3. ////////////////////////////////////////////////////////////////////
  4. //                                                                //
  5. //   Fusion ASP Components                                          //
  6. //   Custom JScript Object: MSDBUpdate                         //
  7. //                                                                //
  8. ///////////////////////////////////////////////////////////////////*
  9. /*
  10. Object:       MSDBUpdate
  11.  
  12. Version:      1.0  9/23/97
  13.  
  14. Written By:   Application Methods, Inc.
  15.               6300 Southcenter Blvd.
  16.               Seattle, WA 98188
  17.               (206) 244-2400
  18.               http://www.appmethods.com
  19.  
  20. Description: The MSDBUpdate button object is responsible for modifying
  21.    the specified record of a table.  The button on the page is a
  22.    standard submit button which points to a special form handler.
  23.    The object passes its properties to the form handler by creating
  24.    hidden fields for each important parameter.  This is done by the
  25.    render method.
  26.  
  27. Note:   Generation of a number of hidden fields is necessary to pass information
  28.    on to other components.  All hidden fields created by this object are prefixed
  29.    with 'amaspHidden_' or 'amaspComponent_'.  Developers should never create
  30.    any form element with these prefixes or unexpected results will occur.
  31.  
  32. Properties:
  33.    name - Name of Update object
  34.    action - Action to be taken on the table
  35.    queryComponent - name of query component from which to extract the Primary key field values
  36.    tableName - Name of the table to edit
  37.  
  38.    successURL - Relative URL of success page
  39.    errorURL - Relative URL of error page
  40.  
  41.    primaryKeyFieldCount - Count of primary key fields
  42.    primaryKeyFieldNames - Names of primary key fields
  43.    primaryKeyFieldValues - Values of primary key fields (optional, may constructed by object)
  44.    primaryKeyFieldTypes - Types of the primary key fields
  45.  
  46.    fieldCount - Count of update fields (optional, may be constructed by form handler)
  47.    fieldNames - Names of update fields (optional, may be constructed by form handler)
  48.    fieldValues - Values of update fields (optional, may be constructed by form handler)
  49.    fieldTypes - Types of the primary key fields (optional, may be constructed by form handler)
  50.  
  51. Methods:
  52.    render - renders necessary hidden fields
  53.    emitProperties - writes all object properties
  54.    getValues - retrieves primary key field values
  55.    buildAddSQL - builds adds SQL
  56.    buildDeleteSQL - builds delete SQL
  57.    buildModifySQL - builds modify SQL
  58.    execute - gets database, builds & executes SQL
  59.    buildWhereSQL  - builds where portion of SQL
  60.    setDatabaseProperties - sets properties for database and ODBC database type
  61.  
  62. Usage:   The following example shows a value which may be edited and updated
  63.    by the user.
  64.  
  65. <HTML>
  66. <HEAD>
  67.  
  68. <!SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
  69.  
  70.    // Create and connect the database object
  71.    MSDBConnection1 = new MSDBConnection("test", "ODBC","MS Access", "Northwind","admin","",false);
  72.    MSDBConnection1.connect();
  73.  
  74.    // Create the query object
  75.    MSDBQuery1 = new MSDBQuery("MSDBQuery1", "MSDBConnection1", false, "*", "Customers", "customerID > 7", "lastname");
  76.  
  77.    // Create the dynafield object
  78.    MSDBDynaField1 = new MSDBDynaField( "MSDBDynaField1",
  79.                             "true",
  80.                             "MSDBQuery1",
  81.                             "",
  82.                             "ShipCity",
  83.                             "string",
  84.                             "false",
  85.                             10,
  86.                             10,
  87.                             1,
  88.                             "Arial",
  89.                             "+0",
  90.                             "black",
  91.                             "false",
  92.                             "false",
  93.                             "false");
  94.    // Create update object
  95.  
  96.    MSDBUpdate1primaryKeyFieldNames = new Array(1);
  97.    MSDBUpdate1primaryKeyFieldDataTypes = new Array(1);
  98.  
  99.    MSDBUpdate1primaryKeyFieldNames[0] = "CustomerID"
  100.    MSDBUpdate1primaryKeyFieldDataTypes[0] = "string"
  101.  
  102.    MSDBUpdate1 = new MSDBUpdate( "MSDBUpdate1", "modify", "MSDBQuery1", "Customers",
  103.                                  "../html/Success.html", "../html/Error.html",
  104.                                  1, MSDBUpdate1primaryKeyFieldNames, null, MSDBUpdate1primaryKeyFieldDataTypes,
  105.                                  null, null, null, null) {
  106.  
  107. <!/SCRIPT>
  108.  
  109. <META NAME="Generator" CONTENT="NetObjects Fusion 2.0 for Windows">
  110.  
  111. </HEAD>
  112. <body>
  113.  
  114.    write("<FORM METHOD=POST>");
  115. <%
  116.    MSDBConnection1.render()
  117.    MSDBDynaField1.render()
  118.    MSDBUpdate1.render()
  119. %>
  120.    write("</FORM>");
  121.  
  122. </BODY>
  123. </HTML>
  124.  
  125.  
  126. =====================================================================*/
  127.  
  128. //
  129. // MSDBUpdate Object Constructor
  130. //
  131. function MSDBUpdate (name, action, queryComponent, tableName,
  132.          successURL, errorURL,
  133.          primaryKeyFieldCount, primaryKeyFieldNames, primaryKeyFieldValues, primaryKeyFieldDataTypes,
  134.          fieldCount, fieldNames, fieldValues, fieldDataTypes ) {
  135.  
  136.    // Properties
  137.    this.name = (name != null) ? name : "";
  138.    this.dataSource = "MSDBConnection1";
  139.    this.action = (action != null) ? action : "";
  140.    this.queryComponent = (queryComponent != null) ? queryComponent : "";
  141.    this.tableName = (tableName != null) ? tableName : "";
  142.    this.databaseType = "";
  143.    this.ODBCDatabaseType = "";
  144.    this.primaryKeyFieldCount = (primaryKeyFieldCount+"" != "null") ? primaryKeyFieldCount: 0;
  145.  
  146.    if (primaryKeyFieldNames != null) {
  147.       this.primaryKeyFieldNames = primaryKeyFieldNames;
  148.    }
  149.    else {
  150.       this.primaryKeyFieldNames = new Array();
  151.       for (var i = 0; i < parseInt(primaryKeyFieldCount,10) - 1; i++) this.primaryKeyFieldNames[i]="";
  152.    }
  153.  
  154.    if (primaryKeyFieldValues != null) {
  155.       this.primaryKeyFieldValues = primaryKeyFieldValues;
  156.    }
  157.    else {
  158.       this.primaryKeyFieldValues = new Array();
  159.       for (var i = 0; i < parseInt(primaryKeyFieldCount) - 1; i++) this.primaryKeyFieldValues[i]="";
  160.    }
  161.  
  162.    if (primaryKeyFieldDataTypes != null) {
  163.       this.primaryKeyFieldDataTypes = primaryKeyFieldDataTypes;
  164.    }
  165.    else {
  166.       this.primaryKeyFieldDataTypes = new Array();
  167.       for (var i = 0; i < parseInt(primaryKeyFieldCount) - 1; i++) this.primaryKeyFieldDataTypes[i]="";
  168.    }
  169.  
  170.    this.successURL = (successURL != null) ? successURL : "";
  171.    this.errorURL = (errorURL != null) ? errorURL : "";
  172.  
  173.    this.fieldCount = ((fieldCount+"" != "null") && (fieldCount+"" != "undefined") && (fieldCount != null)) ? fieldCount : 0;
  174.  
  175.    if (fieldNames != null) {
  176.       this.fieldNames = fieldNames;
  177.    }
  178.    else {
  179.       this.fieldNames = new Array();
  180.       for (var i = 0; i < this.fieldCount - 1; i++) this.fieldNames[i]="";
  181.    }
  182.  
  183.    if (fieldValues != null) {
  184.       this.fieldValues = fieldValues;
  185.    }
  186.    else {
  187.       this.fieldValues = new Array();
  188.       for (var i = 0; i < this.fieldCount - 1; i++) this.fieldValues[i]="";
  189.    }
  190.  
  191.    if (fieldDataTypes != null) {
  192.       this.fieldDataTypes = fieldDataTypes;
  193.    }
  194.    else {
  195.       this.fieldDataTypes = new Array();
  196.       for (var i = 0; i < this.fieldCount - 1; i++) this.fieldDataTypes[i]="";
  197.    }
  198.  
  199.    // Methods
  200.    this.render = utRender;
  201.    this.emitProperties = utEmitProperties;
  202.    this.getValues = utGetValues;
  203.    this.buildAddSQL = utBuildAddSQL;
  204.    this.buildDeleteSQL = utBuildDeleteSQL;
  205.    this.buildModifySQL = utBuildModifySQL;
  206.    this.execute = utExecute;
  207.    this.buildWhereSQL = utBuildWhereSQL;
  208.    this.setDatabaseTypes = utSetDatabaseTypes;
  209.  
  210.    if (this.action != "add") {
  211.       // Increment global cursor usage counter since this component will try to use the cursor
  212.       incCursorCallCount();
  213.    }
  214.  
  215. }  // END MSDBUpdate constructor
  216.  
  217.  
  218. //
  219. // This method will first check primary key fields for existance then fill the
  220. // Primary Key Field Value array with the corresponding values for each field.
  221. // Errors are printed for null cursor, failure to advance to first record as
  222. // necessary and non-existance of field in query.
  223. //
  224. function utGetValues() {
  225.  
  226.     //debug("Getting field values")
  227.  
  228.    var fieldError = true;
  229.  
  230.    var fcnt;
  231.    var ccnt;
  232.  
  233.    //debug("Getting cursor from query '"+ this.queryComponent + "'");
  234.    myCursor = eval(this.queryComponent + ".getCurrentCursor()");
  235.    if (myCursor != void(0)) {
  236.      if (myCursor != null) {
  237.  
  238.        if (eval(this.queryComponent + ".initializeCursor()")) {
  239.  
  240.          //Check primary key field names for existance
  241.          //debug("searching fields");
  242.          for (fcnt = 0; fcnt <  parseInt(this.primaryKeyFieldCount,10); fcnt++) {
  243.              fieldError=true;
  244.             for (ccnt = 0; ccnt < myCursor.Fields.Count; ccnt++) {
  245.                if (myCursor.Fields(ccnt).Name.toUpperCase() == this.primaryKeyFieldNames[fcnt].toUpperCase()) {
  246.                fieldError=false;
  247.                } // end if
  248.              } // end if fields for
  249.              if (fieldError) {
  250.                debug("field error found");
  251.                 write("\n<P>***Warning: Primary key field name: '" + this.primaryKeyFieldNames[fcnt] + "' not found in query");
  252.             }
  253.          }
  254.  
  255.          // get values for each primary key field
  256.          for (var i = 0; i < parseInt(this.primaryKeyFieldCount,10); i++) {
  257.             this.primaryKeyFieldValues[i]=formatDataValue(myCursor.Fields(this.primaryKeyFieldNames[i]).Value, this.primaryKeyFieldDataTypes[i]);
  258.          }
  259.         }
  260.         else {
  261.           // write("\n<P>***Warning: No records found when attempting to get primary key values.");
  262.          }
  263.        }
  264.        else {
  265.           write("\n<P>***Warning: Unable to obtain a valid cursor when attempting to get primary key values.");
  266.        }
  267.      }
  268.      else {
  269.         write("\n<P>***Warning: Unable to obtain a valid cursor.");
  270.      }
  271.  
  272.  
  273. }  // END utCheckFieldErrors
  274.  
  275.  
  276. //
  277. // This method renders all hidden fields from object properties
  278. //
  279. function utRender() {
  280.  
  281.     //debug("Rendering hidden fields")
  282.  
  283.    if (this.action != "add")
  284.       this.getValues();
  285.  
  286.    //this.emitProperties();
  287.    // Temporary - Change later
  288.    var pre = "amaspHidden_"+this.name+"_";
  289.  
  290.    // Emit other properties
  291.    write("\n<INPUT NAME = \""+pre+"tableName\" VALUE = \"" + this.tableName + "\" TYPE = \"HIDDEN\">");
  292.    write("\n<INPUT NAME = \""+pre+"primaryKeyFieldCount\" VALUE = \"" + this.primaryKeyFieldCount + "\" TYPE = \"HIDDEN\">");
  293.    write("\n<INPUT NAME = \""+pre+"successURL\" VALUE = \"" + this.successURL + "\"  TYPE = \"HIDDEN\">");
  294.    write("\n<INPUT NAME = \""+pre+"errorURL\" VALUE = \"" + this.errorURL + "\" TYPE = \"HIDDEN\">");
  295.    write("\n<INPUT NAME = \""+pre+"action\" VALUE = \"" + this.action + "\" TYPE = \"HIDDEN\">");
  296.  
  297.    // Emit primary key fields
  298.    if (this.action != "add") {
  299.       for (var i = 0; i < parseInt(this.primaryKeyFieldCount,10); i++) {
  300.          write("\n<INPUT NAME = \"" + pre + "primaryKeyFieldNames"+ i + "\" VALUE = \"" + this.primaryKeyFieldNames[i] + "\" TYPE = \"HIDDEN\">");
  301.          write("\n<INPUT NAME = \""  + pre + "primaryKeyFieldValues"+ i + "\" VALUE = \"" + this.primaryKeyFieldValues[i] + "\" TYPE = \"HIDDEN\">");
  302.          write("\n<INPUT NAME = \""  + pre + "primaryKeyFieldDataTypes"+ i + "\" VALUE = \"" + this.primaryKeyFieldDataTypes[i] + "\" TYPE = \"HIDDEN\">");
  303.       }
  304.    }
  305.  
  306.     write("\n");
  307.  
  308.     if (this.action != "add") {
  309.       // Decrement global cursor usage counter since this component has tried to use the cursor
  310.       // If the counter has reached zero after doing so, then this is the last cursor using
  311.       // component on the page, so call the DBQuery object's "cursorClose()" method to close
  312.       // the cursor if it's still open.
  313.          decCursorCallCount(this.queryComponent);
  314.    } // end if
  315.  
  316. }  // END utrender
  317.  
  318.  
  319. //
  320. //  This method outputs all component properties
  321. //
  322. function utEmitProperties () {
  323.  
  324.       debug("Writing DB Update Button properties...");
  325.       write("\n<BR><B>MSDBUpdate Properties:</B>");
  326.       write("\n<BR>name = " + this.name);
  327.       write("\n<BR>action = " + this.action);
  328.       write("\n<BR>queryComponent = " + this.queryComponent);
  329.       write("\n<BR>tableName = " + this.tableName);
  330.       write("\n<BR>successURL = " + this.successURL);
  331.       write("\n<BR>errorURL = " + this.errorURL);
  332.       write("\n<BR>primaryKeyFieldCount = " + this.primaryKeyFieldCount);
  333.       var i;
  334.       for (i = 0; i < parseInt(this.primaryKeyFieldCount,10); i++) {
  335.          write("\n<BR>primaryKeyFieldNames" + i + " = " + this.primaryKeyFieldNames[i]);
  336.          write("\n<BR>primaryKeyFieldValues" + i + " = " + this.primaryKeyFieldValues[i]);
  337.          write("\n<BR>primaryKeyFieldDataTypes" + i + " = " + this.primaryKeyFieldDataTypes[i]);
  338.       }
  339.  
  340.      write("\n<BR>fieldCount = " + this.fieldCount);
  341.      for (i = 0; i < parseInt(this.fieldCount,10); i++) {
  342.          write("\n<BR>fieldNames" + i + " = " + this.fieldNames[i]);
  343.          write("\n<BR>fieldValues" + i + " = " + this.fieldValues[i]);
  344.          write("\n<BR>fieldDataTypes" + i + " = " + this.fieldDataTypes[i]);
  345.       }
  346.       write("<BR>");
  347.  
  348. }  // END utEmitProperties
  349.  
  350.  
  351. //
  352. //  Builds where portion of SQL.
  353. //  SQL Syntax: WHERE PKN1=PKV1 and PKN2=PKV2
  354. //
  355. function utBuildWhereSQL () {
  356.  
  357.   var sql;
  358.  
  359.   //debug("Building WHERE portion of SQL");
  360.   if (parseInt(this.primaryKeyFieldCount,10) < 1) debug("0 Primary key fields counted or found");
  361.  
  362.   // SQL Syntax: WHERE PKN1=PKV1 and PKN2=PKV2
  363.   sql = "WHERE";
  364.   for (var fcnt=0; fcnt<parseInt(this.primaryKeyFieldCount,10); fcnt++) {
  365.  
  366.      sql += " " + this.primaryKeyFieldNames[fcnt] + " = " +
  367.            convertForInput(this.primaryKeyFieldDataTypes[fcnt],wrapDelim(this.databaseType, this.primaryKeyFieldDataTypes[fcnt],this.primaryKeyFieldValues[fcnt]));
  368.     if (fcnt < parseInt(this.primaryKeyFieldCount,10)-1) sql += " AND"
  369.   }
  370.  
  371.   return sql;
  372.  
  373. }  // END utBuildWhereSQL
  374.  
  375.  
  376. //
  377. //  Builds add SQL.
  378. //  SQL Syntax: "INSERT TableName (FN1,FN1,FN1) VALUES ("FV1", "FV2", FV3)
  379. //
  380. function utBuildAddSQL () {
  381.  
  382.   var sql;
  383.   var sqlNames = "";
  384.   var sqlValues = "";
  385.  
  386.   //debug("Building add SQL");
  387.   if (this.fieldCount < 0) debug("0 Primary key fields counted or found");
  388.  
  389.   // SQL Syntax: "INSERT TableName (FN1,FN1,FN1) VALUES ("FV1", "FV2", FV3)
  390.   for (var fcnt=0; fcnt<this.fieldCount; fcnt++) {
  391.  
  392.     sqlNames += this.fieldNames[fcnt];
  393.     sqlValues += " " + convertForInput(this.fieldDataTypes[fcnt],wrapDelim(this.databaseType, this.fieldDataTypes[fcnt],this.fieldValues[fcnt]));
  394.     if (fcnt < this.fieldCount-1) {
  395.       sqlNames += ", ";
  396.       sqlValues += ",";
  397.     }
  398.   }
  399.   sql = "INSERT INTO " + this.tableName + " ("+sqlNames+") VALUES ("+sqlValues+")";
  400.  
  401.   return sql;
  402.  
  403. }  // END utBuildAddSQL
  404.  
  405.  
  406. //
  407. //  Builds delete SQL.
  408. //  SQL Syntax: "DELETE Table WHERE PKN1=PKV1 and PKN2=PKV2
  409. //
  410. function utBuildDeleteSQL () {
  411.   var sql;
  412.  
  413.   //debug("Building delete SQL:");
  414.  
  415.   // SQL Syntax: "DELETE Table WHERE PKN1=PKV1 and PKN2=PKV2
  416.   sql = "DELETE FROM " + this.tableName + " " + this.buildWhereSQL();
  417.  
  418.   return sql;
  419.  
  420. }  // END utBuildDeleteSQL
  421.  
  422.  
  423. //
  424. //  Builds modify SQL.
  425. //  SQL Syntax: "UPDATE TableName SET (FN1=FV1, FN2=FV2, FN3=FV3) WHERE PKN1=PKV1 and PKN2=PKV2
  426. //
  427. function utBuildModifySQL () {
  428.   var sql;
  429.   var sqlPairs = "";
  430.  
  431.   //debug("Building modify SQL:");
  432.  
  433.   // SQL Syntax: "UPDATE TableName SET (FN1=FV1, FN2=FV2, FN3=FV3) WHERE PKN1=PKV1 and PKN2=PKV2
  434.   for (var fcnt=0; fcnt<this.fieldCount; fcnt++) {
  435.  
  436.     sqlPairs += this.fieldNames[fcnt] + " = " +
  437.              convertForInput(this.fieldDataTypes[fcnt],wrapDelim(this.databaseType, this.fieldDataTypes[fcnt],this.fieldValues[fcnt]));
  438.     if (fcnt < this.fieldCount-1) {
  439.       sqlPairs += ", ";
  440.     }
  441.   }
  442.   sql = "UPDATE " + this.tableName + " SET "+sqlPairs+" " + this.buildWhereSQL();
  443.  
  444.   return sql;
  445.  
  446. }  // END utBuildModifySQL
  447.  
  448.  
  449. //
  450. //  Gets database, executes the SQL
  451. //
  452. function utExecute () {
  453.  
  454.   var sql = "";
  455.  
  456.   if (Application("globalConn") != "true") {
  457.       write("\r\n<BR>Connecting to database...");
  458.       //debug("Connecting to database...");
  459.       eval(this.dataSource + ".connect()");
  460.   }
  461.   else { // Assume connected
  462.         write("\r\n<BR>Using existing database connection...");
  463.       //debug("Using existing database connection...");
  464.   }
  465.  
  466.    // build and execute: "INSERT TableName (FN1,FN1,FN1) VALUES ("FV1", "FV2", FV3)
  467.    if (Session(this.dataSource) != null) {
  468.       if (this.action=="add") sql = this.buildAddSQL();
  469.       if (this.action=="modify") sql = this.buildModifySQL();
  470.       if (this.action=="delete") sql = this.buildDeleteSQL();
  471.  
  472.          //debug("Executing SQL");
  473.          var resultPage =  (ExecuteSQL(this.dataSource,sql)) ? upd1.successURL : upd1.errorURL
  474.          debug("\r\nRedirecting to: " + resultPage);
  475.          if (Application("globalConn") != "true")
  476.             eval(this.dataSource + ".disconnect()");
  477.          Response.Clear();
  478.          Response.Redirect (resultPage);
  479.          Response.End();
  480.  
  481.    }
  482.    else {
  483.       write("\r\n<BR>Unable to open a valid database connection...");
  484.       debug("Unable to open a valid database connection...");
  485.       Response.Clear();
  486.       Response.Redirect (upd1.errorURL);
  487.       Response.End();
  488.    }
  489.  
  490.  
  491. }  // END utExecute
  492.  
  493.  
  494. //
  495. // Set parameters for database types after validating them
  496. //
  497. function utSetDatabaseTypes(databaseType, ODBCDatabaseType) {
  498.  
  499.    this.databaseType = (databaseType == null) ? "" : databaseType;
  500.    this.ODBCDatabaseType = (ODBCDatabaseType == null) ? "" : ODBCDatabaseType;
  501.  
  502. }  // End utSetDatabaseTypes
  503.  
  504.  
  505. </SCRIPT>
  506.  
  507. <SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
  508.  
  509. function ExecuteSQL (DS,SQL)
  510.     'Returns successfullness
  511.  
  512.     on error resume next
  513.  
  514.     Session(DS).Errors.Clear()
  515.  
  516.     'debug("\nExecuting: " + SQL)
  517.      Session(DS).Execute(SQL)
  518.  
  519.     if Session(DS).Errors.Count > 0 then
  520.         Session(DS).Errors.Clear()
  521.         ExecuteSQL = False
  522.     else
  523.         ExecuteSQL = True
  524.     end if
  525.  
  526. end function
  527. </SCRIPT>
  528.  
  529.  
  530.